import Example from '~/components/example'
import { Code, InlineCode } from '~/components/text/code'
import Link from '~/components/text/link'
import {
  InputTable,
  OutputTable,
  Row,
  Cell,
  TypeCell,
  BoldCell,
  BooleanCell
} from '~/components/api/table'
import Endpoint from '~/components/api/endpoint'
import Request from '~/components/api/request'

export const meta = {
  editUrl: 'pages/docs/api/v1/api-docs-mdx/endpoints/authentication.mdx',
  lastEdited: '2019-01-14T17:02:44.000Z'
}

## Authentication

### Request a login

<Endpoint method="POST" url="/now/registration" />

Request a new login for an user to get a token.

#### Input

<InputTable>
  <Row>
    <BoldCell>email</BoldCell>
    <TypeCell>String</TypeCell>
    <BooleanCell status={true} />
    <Cell>The user email.</Cell>
  </Row>
  <Row>
    <BoldCell>tokenName</BoldCell>
    <TypeCell>String</TypeCell>
    <BooleanCell status={true} />
    <Cell>
      The desired name for the token. It will be displayed on the{' '}
      <Link href="https://zeit.co/account/tokens">user account details</Link>.
    </Cell>
  </Row>
</InputTable>

### Output

<OutputTable>
  <Row>
    <BoldCell>token</BoldCell>
    <TypeCell>String</TypeCell>
    <Cell>The token used to verify the user accepted the login request.</Cell>
  </Row>
  <Row>
    <BoldCell>securityCode</BoldCell>
    <TypeCell>String</TypeCell>
    <Cell>
      The code the user is going to receive on the email. <strong>Must</strong>{' '}
      be displayed to the user so they can verify the request is the correct.
    </Cell>
  </Row>
</OutputTable>

<Example>
  Example request:

<Request
  method="POST"
  url="https://api.zeit.co/now/registration"
  headers={{
    'Content-Type': 'application/json'
  }}
  body={{
    email: 'user@mail.com',
    tokenName: 'Your Client App Name'
  }}
/>

Example response:

<Code lang="json">{`{
    "token": "T1dmvPu36nmyYisXAs7IRzcR",
    "securityCode": "Practical Saola"
  }`}</Code>
</Example>

### Verify login

<Endpoint method="GET" url="/now/registration/verify?email&token" />

Verify the user accepted the login request and get a authentication token. The user email address and the token received after [requesting the login](#endpoints/authentication/request-a-login) must be added to the URL as a query string with the names `email` and `token`.

#### Output

<OutputTable>
  <Row>
    <BoldCell>token</BoldCell>
    <TypeCell>String</TypeCell>
    <Cell>
      The user authentication token you can use as described in{' '}
      <Link href="#api-basics/authentication">API Basics - Authentication</Link>.
    </Cell>
  </Row>
</OutputTable>

<Example>
  Example request:

<Request url="https://api.zeit.co/now/registration/verify?email=user@mail.com&token=T1dmvPu36nmyYisXAs7IRzcR" />

Example response:

<Code lang="json">{`{
    "token": "sGkHhSH98wtJB0lyODZJ2bRe"
  }`}</Code>
</Example>
